package com.posjiajia.user.core.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类转换工具
 * @Title BeanTransferUtils.java
 * @Description 类转换工具
 * @author zhenhua.sun
 * @date 2016年3月2日 上午10:13:14
 * @version V1.0
 */
public class BeanTransferUtil {

    private static final Logger logger = LoggerFactory.getLogger(BeanTransferUtil.class);

    public static <T> T transferType(Object source,Class<T> targetClass){
        T target = null;
        try{
            target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
        }catch(Exception e){
            logger.error("bean transfer utils error:", e);
        }
        return target;
    }

    public static <T> List<T> batchTransferType(List sourceList, Class<T> targetClass)  {
        List<T> targetList = new ArrayList<T>();
        for (int i = 0; i < sourceList.size(); i++) {
            Object source = sourceList.get(i);
            try {
                T target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target);
                targetList.add(target);
            } catch (Exception e) {
                logger.error("bean transfer error:", e);
            }
        }
        return targetList;
    }
}
